package cn.edu.xhu.library_management.department.controller;

import cn.edu.xhu.library_management.common.entity.R;
import cn.edu.xhu.library_management.department.entity.LmFaculty;
import cn.edu.xhu.library_management.department.entity.search.SearchFaculty;
import cn.edu.xhu.library_management.common.validator.ValidationGroups;
import cn.edu.xhu.library_management.department.service.FacultyService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: MMJ
 * @Date: 2022-01-28
 * @Description: 院系Controller restful风格
 */
@Api(tags = "院系模块")
@ApiSort(1)
@RestController
public class FacultyController {
    @Autowired
    private FacultyService facultyService;
    // 查询所有的院系实体类
    @ApiOperation(value = "查询所有院系",produces="application/json")
    @ApiOperationSupport(order = 111)
    @GetMapping("lm_department/faculty")
    public R findAllFaculties(){
        List<LmFaculty> faculties = facultyService.findAllFaculties();
        return !faculties.isEmpty()?R.ok().data("faculties",faculties):R.error("数据为空!!!");
    }

    // 通过院系ID查询院系
    @ApiOperation(value = "通过院系ID查询院系",produces="application/json")
    @ApiOperationSupport(order = 112)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "院系ID")
    })
    @GetMapping("lm_department/faculty/{id}")
    public R findFacultyById(@PathVariable("id") Integer id){
        LmFaculty faculty = facultyService.findFacultyById(id);
        return faculty != null ? R.ok().data("faculty",faculty):R.error("查无此数据!!!");
    }

    // 搜索院系(带分页)
    @ApiOperation(value = "搜索院系(分页)",produces="application/json")
    @ApiOperationSupport(order = 113)
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "faculty",value = "院系搜索对象"),
            @ApiImplicitParam(name = "currentPage",value = "当前页"),
            @ApiImplicitParam(name = "currentPage",value = "每页条数")
    })
    @GetMapping("lm_department/faculty/search")
    public R search(SearchFaculty searchFaculty) {
        Map<String, Object> map = facultyService.search(searchFaculty);
        return R.ok().data(map);
    }

    // 添加院系，单个添加
    @ApiOperation(value = "添加院系(单个)",produces="application/json")
    @ApiOperationSupport(order = 121)
    @PostMapping("/lm_department/faculty")
    public R addFaculty(@Validated @RequestBody LmFaculty faculty){
        return facultyService.addFaculty(faculty)? R.ok("添加院系成功!!!"):R.error("添加院系失败!!!");
    }

    // 修改院系
    // @Validated(ValidationGroups.LmFacultyUpdate.class) 使用validator 分组检验，没有则是默认检验
    @ApiOperation(value = "修改院系",produces="application/json")
    @ApiOperationSupport(order = 131)
    @PutMapping("/lm_department/faculty")
    public R updateFaculty(@Validated(ValidationGroups.LmFacultyUpdate.class) @RequestBody LmFaculty faculty){
        return facultyService.updateFaculty(faculty)?R.ok("修改院系成功!!!"):R.error("修改院系失败!!!");
    }

    // 通过院系ID删除院系
    @ApiOperation(value = "删除院系(院系ID)",produces="application/json")
    @ApiOperationSupport(order = 141)
    @DeleteMapping("/lm_department/faculty/{id}")
    public R deleteFaculty(@PathVariable("id") Integer faId){
        return facultyService.deleteFacultyById(faId)?R.ok("删除院系成功!!!"):R.error("删除院系失败!!!");
    }

    // 通过院系IDs删除院系
    @ApiOperation(value = "删除院系(院系IDs)",produces="application/json")
    @ApiOperationSupport(order = 142)
    @DeleteMapping("/lm_department/faculty")
    public R deleteFaculties(@RequestParam("faIds") List<Integer> faIds){

        //@RequestParam("classIds") List<Integer> schoolClassIDs
        return facultyService.deleteFacultiesByIds(faIds)?R.ok("批量删除院系成功!!!"):R.error("批量删除院系失败!!!");
    }

}
